package zooin.net;

import android.app.Activity;
import android.app.AlertDialog;
import zooin.net.R;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;

public class MainCategory extends Activity {
	private ImageButton liveButton;
	private ImageButton vodButton;
	public function fc = new function();
	private String pId,pDeviceIdx,thisIP,thisM ,dMode;
	private SharedPreferences pref=null;
	public static String INTENT_USER="userid";
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_category);

        pref = getSharedPreferences("userInfo", MODE_PRIVATE);
	        pId 			= pref.getString("pId", "");
	        pDeviceIdx 	= pref.getString("pDeviceIdx", "");
	        thisIP 		= pref.getString("thisIP", "");
	    
	    if(thisIP=="" | thisIP==null){
	    	SharedPreferences.Editor editor = pref.edit();
	    	editor.putString("thisIP", fc.IP);
	    	editor.putString("thisM",fc.getDeviceModel());	
	    	editor.commit();
	    }
	  //  Toast.makeText(MainCategory.this, "pUserIdx : "+fc.getDeviceModel(), Toast.LENGTH_SHORT).show();
        intenter();

		//생방송-live
		liveButton = (ImageButton) findViewById(R.id.Live);
		liveButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(MainCategory.this,LiveList.class);
	        		//intent.putExtra(INTENT_VAL, postUserIdx+","+postId+","+postName+","+postDeviceIdx+","+postIP);
				startActivity(intent);
				//finish();
			}
		});
		//VOD
		vodButton = (ImageButton) findViewById(R.id.Vod);
		vodButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
			
					Intent intent = new Intent(MainCategory.this,VodCategory.class);
	        			//intent.putExtra(INTENT_VAL, postUserIdx+","+postId+","+postName+","+postDeviceIdx+","+postIP);
					startActivity(intent);

			}
		});
	
    }
    

    public void intenter(){
    	Intent intent = getIntent();
    	
    }
    
  //매뉴 만들기
    public boolean onCreateOptionsMenu(Menu menu) {
    	boolean result = super.onCreateOptionsMenu(menu);
    		MenuInflater menuInflator = new MenuInflater(this);
    		menuInflator.inflate(R.menu.m_setting, menu);
    	return result;
    }
    //menu 이벤트처리
    public boolean onOptionsItemSelected(MenuItem item){
    	switch (item.getItemId()) {
		case R.id.menu_set:	//정보수정
			Intent intent = new Intent(MainCategory.this,setList.class);
				intent.putExtra(INTENT_USER, "unknown");
			startActivity(intent);
			//finish();
			return true;
		}
    	return (super.onOptionsItemSelected(item));
    }
    
    
    //Back 버튼
    @Override
    public void onBackPressed() {
    	//finish();
    	alertEnd();
		//MainCategory.this.finish();
	}
    
    //종료 물어봄.
    public void alertEnd(){
    	
        pref = getSharedPreferences("userInfo", MODE_PRIVATE);
	    AlertDialog.Builder alt_bld = new AlertDialog.Builder(MainCategory.this);
	    alt_bld.setMessage("정말 종료하시겠습니까?").setCancelable(
	        false).setPositiveButton("네",new DialogInterface.OnClickListener() {
	        	public void onClick(DialogInterface dialog, int id) {
	        		// Action for 'Yes' Button

	        		/*ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
	        		am.restartPackage(getPackageName());*/
	        		//ActivityManager am = (ActivityManager)getSystemService(ACTIVITY_SERVICE);
	        		//am.killBackgroundProcesses(getPackageName());

	        		   SharedPreferences.Editor editor = pref.edit();
	    		        //ID 초기화
	    		        editor.remove("pUserIdx");
	    		        editor.remove("pId");
	    		        editor.remove("pName");
	    		        editor.remove("pDeviceIdx");
	    		        editor.remove("pDeviceName");
	    		        editor.remove("thisIP");
	    		        editor.remove("thisM");
	    		        editor.remove("dMode");
	    		        editor.remove("loginok");
	    		        editor.commit();
	        		
	        		android.os.Process.killProcess(android.os.Process.myPid());
	        		MainCategory.this.finish();

	            	System.exit(0);
	        	}
	        }).setNegativeButton("아니오",new DialogInterface.OnClickListener() {
	        	public void onClick(DialogInterface dialog, int id) {
	        		// Action for 'NO' Button
	        		dialog.cancel();
	        	}

	        });

	    AlertDialog alert = alt_bld.create();
	    // Title for AlertDialog
	    alert.setTitle("종료");
	    // Icon for AlertDialog
	    alert.setIcon(R.drawable.icon);
	    
	    alert.show();
	}
    
   @Override
    public void onDestroy() {
    	super.onDestroy();
    	super.finish();
    	System.exit(0);
    }  
   

}